package testJbossJms.remote.main;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class RemoteProducer {
	public static void main(String[] args) {
		try {
			Context context = new InitialContext();
			ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup("jms/RemoteConnectionFactory");
			Queue queue = (Queue)context.lookup("jms/queue/queue1");
			
			Connection connection;
			try {
				connection = connectionFactory.createConnection("user1", "User1@123");
				Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
				MessageProducer producer = session.createProducer(queue);
				for(int i = 20; i<30; i++){
					connection.start();
					TextMessage message = session.createTextMessage("this is a message.." + i);
					producer.send(message);
				}
				connection.close();
				
				session.close();
				
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
